package core;

import java.util.HashMap;
import java.util.Vector;

/**
 * @author Gary
 */
public abstract class GameMode {

    // Server Information
    protected GameServer gameServer;
    // Mode Information
    protected int mode_id;
    protected short mode_type;
    protected String map_id;
    // Other Information
    protected Vector<GameCharacter> playerList;
    protected HashMap<Integer, GameCharacter> playerLoadingList;
    protected int startCountdown;
    protected int endCountdown;
    protected GameTimer startTimer;
    protected GameTimer endTimer;

    public GameMode() {
        playerList = new Vector<GameCharacter>();
        playerLoadingList = new HashMap<Integer, GameCharacter>();

        startCountdown = 5000;
        endCountdown = 5000;

        startTimer = new GameTimer();
        endTimer = new GameTimer();
    }

    public GameServer getGameServer() {
        return gameServer;
    }

    public GameServer setGameServer(GameServer gameServer) {
        return this.gameServer = gameServer;
    }

    public int getID() {
        return mode_id;
    }

    public int setID(int mode_id) {
        return this.mode_id = mode_id;
    }

    public short getModeType() {
        return mode_type;
    }

    public short setModeType(short mode_type) {
        return this.mode_type = mode_type;
    }

    public String getMapID() {
        return map_id;
    }

    public String setMapID(String map_id) {
        return this.map_id = map_id;
    }

    public GameCharacter getPlayer(int char_id) {
        GameCharacter character = null;

        for (GameCharacter player : playerList) {
            if (player.getID() == char_id) {
                character = player;
                break;
            }
        }

        return character;
    }

    public void setPlayer(GameCharacter character) {
        playerList.add(character);
    }

    public GameCharacter removePlayer(int char_id) {
        GameCharacter character = getPlayer(char_id);

        if (character != null) {
            playerList.remove(character);
        }

        return character;
    }

    public Vector<GameCharacter> getPlayerList() {
        return playerList;
    }

    public Vector<GameCharacter> setPlayerList(Vector<GameCharacter> playerList) {
        return this.playerList = playerList;
    }

    public int setPlayerReady(int char_id) {
        if (playerLoadingList.containsKey(char_id)) {
            playerList.add(playerLoadingList.remove(char_id));
        }

        return playerLoadingList.size();
    }

    public int getStartCountdown() {
        return startCountdown;
    }

    public int setStartCountdown(int startCountdown) {
        return this.startCountdown = startCountdown;
    }

    public int getEndCountdown() {
        return endCountdown;
    }

    public int setEndCountdown(int endCountdown) {
        return this.endCountdown = endCountdown;
    }

    public GameTimer getStartTimer() {
        return startTimer;
    }

    public abstract void setStartTimer();

    public void removeStartTimer() {
        if (startTimer.getTask() != null) {
            startTimer.getTask().cancel();
        }
    }

    public GameTimer getEndTimer() {
        return endTimer;
    }

    public abstract void setEndTimer();

    public void removeEndTimer() {
        if (endTimer.getTask() != null) {
            endTimer.getTask().cancel();
        }
    }

    public void end() {
        if (startTimer.getTask() != null) {
            startTimer.getTask().cancel();
        }

        if (endTimer.getTask() != null) {
            endTimer.getTask().cancel();
        }

        for (GameCharacter character : playerList) {
            character.setGameMode(0);
        }
    }
}
